<?php

/**
 * Class Core_I18N
 * @package Core
 */
class Core_I18N {

    protected static $_res = array();

    public static function _($str, $module = 'global', $params = []) {

        $lang = Core_Request::getInstance()->getLang();

        $translation = self::getTranslation($str, $module, $lang);

        if ($params) {
            $params = (array)$params;
            if (isset($params[$lang])) {
                $params = $params[$lang];
            }
            array_unshift($params, $translation);
            return call_user_func_array('sprintf', $params);
        } else {
            return $translation;
        }
    }

    protected static function getTranslation($str, $module, $lang) {
        if (!isset(self::$_res[$lang])) {
            self::$_res[$lang] = array();
        }
        if (!isset(self::$_res[$lang][$module])) {
            self::$_res[$lang][$module] = require PROJECT_PATH . '/Lang/' . $lang . '/' . $module . '.php';
        }
        return isset(self::$_res[$lang][$module][$str]) ? self::$_res[$lang][$module][$str] : $str;
    }
}
